﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using HandyControl.Controls;
using MyHelper;
using clf.weighing.entity;
using CheckBox = System.Windows.Controls.CheckBox;
using RadioButton = System.Windows.Controls.RadioButton;

namespace weighing
{
    /// <summary>
    /// ReportSummaryWindow.xaml 的交互逻辑
    /// </summary>
    public partial class ReportSummaryWindow : GlowWindow
    {
        #region variables
        private bool isMonthReportFirstSelectYear = false;
        public readonly static int DayReport = 0;
        public readonly static int MonthReport = 1;
        public readonly static int YearReport = 2;
        private const string buyName = "BuyWeighingRb";
        private const string SaleName = "SaleWeighingRb";
        private const string SelfName = "SelfWeighingRb";
        private const string foreginName = "ForeginWeighingRb";

        private WeightingBillType billType = WeightingBillType.UNKNOWN;
        #endregion

        #region property
        /// <summary>
        /// 0 月报表 1 年报表
        /// </summary>
        public int ReportType { get; set; }

        #endregion
        public ReportSummaryWindow(int type = 0)
        {
            InitializeComponent();

            CommonFunction.ShowCustomIcon(this);

            ReportType = type;

            if (ReportType == YearReport)
            {
                this.monthCb.Visibility = Visibility.Collapsed;
                this.DayCb.Visibility = Visibility.Collapsed;

                this.GroupDayCb.Visibility = Visibility.Collapsed;
                this.ByMonthRb.Visibility = Visibility.Collapsed;
                this.ByTimeRangeRb.IsChecked = false;
                this.ByTimeRangeRb.Visibility = Visibility.Collapsed;
                
            }
            else if (ReportType == MonthReport)
            {
                this.ByMonthRb.Visibility = Visibility .Visible;
                this.ByTimeRangeRb .Visibility = Visibility .Visible;
                this.ByMonthRb.IsChecked = true;
                this.monthCb.Visibility = Visibility.Visible;
                this.DayCb.Visibility = Visibility.Collapsed;
                this.GroupMonthCb.Visibility = Visibility.Collapsed;
            }
            else
            {
                this.monthCb.Visibility = Visibility.Visible;
                this.DayCb.Visibility = Visibility.Visible;

                this.GroupMonthCb.Visibility = Visibility.Collapsed;
                this.GroupDayCb.Visibility = Visibility.Collapsed;
                this.ByMonthRb.Visibility = Visibility.Collapsed;
                this.ByTimeRangeRb.IsChecked = false;
                this.ByTimeRangeRb.Visibility = Visibility.Collapsed;
            }
            if (ReportType == YearReport)
            {
                this.Title = "年 " + this.Title;
            }
            else if (ReportType == MonthReport)
            {
                this.Title = "月 " + this.Title;
            }
            else
            {
                this.Title = "日 " + this.Title;
            }
        }

        #region win event        

        private void Summary_Report_Win_Loaded(object sender, RoutedEventArgs e)
        {
            SetYearAndMonthItems();


            BuildWeighingTypeRadioButton();

        }

        private void Summary_Report_Win_ContentRendered(object sender, EventArgs e)
        {
            this.exclHelder.Text = App.CurrentCompanyVo?.FullName??"XXX使用公司";
            this.PrintTimeTb.Text = DateTimeHelper.getCurrentDateTime();

            SetDefaultSource();

            // default select buy weiging bill data
            this.BuyWeighingRb.IsChecked = true;

            if (this.yearCb.Items.Count > 0)
            {
                //select last 
                this.yearCb.SelectedIndex = this.yearCb.Items.Count - 1;
            }

            if (this.monthCb.Items.Count > 0)
            {
                //select last 
                this.monthCb.SelectedIndex = this.monthCb.Items.Count - 1;
            }

        }

        private void Summary_Report_Win_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            switch (e.Key)
            {
                case Key.Escape:
                    this.Close();
                    break;
                default:
                    break;
            }
        }
        #endregion

        #region Supply 
        private bool isSupplySelected = false;
        private void SupplyCb_TextChanged(object sender, TextChangedEventArgs e)
        {
            String text = this.sendCompanyCb.Text.Trim();
            if (text.Length < 2)
            {
                isSupplySelected = false;
                this.sendCompanyCb.IsDropDownOpen = false;
            }
            if (isSupplySelected == true) { return; }
            if (text.Length >= 2)
            {
                List<Company> list = CompanyDao.Instance.SelectByNameOrFirstCaseLike(text);
                var data = list?.Select((i) => { return new CompanyVo(i); }).ToList();
                isSupplySelected = list?.Count <= 0;
                this.sendCompanyCb.ItemsSource = data;
                this.sendCompanyCb.IsDropDownOpen = sendCompanyCb.ItemsSource != null;
            }
        }
        private void SupplyCb_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (sendCompanyCb.SelectedIndex > -1) isSupplySelected = true;
        }

        #endregion

        #region Receiver Company
        private bool isSelectReceiveCompany = false;
        private void ReceiverCompanyCb_TextChanged(object sender, TextChangedEventArgs e)
        {
            String text = this.RecevieCompanyCb.Text.Trim();
            if (text.Length < 2)
            {
                isSelectReceiveCompany = false;
                this.RecevieCompanyCb.IsDropDownOpen = false;
            }
            if (isSelectReceiveCompany == true) { return; }
            if (text.Length >= 2)
            {
                List<Company> list = CompanyDao.Instance.SelectByNameOrFirstCaseLike(text);
                var data = list?.Select((i) => { return new CompanyVo(i); }).ToList();
                isSelectReceiveCompany = list?.Count > 0;
                this.RecevieCompanyCb.IsDropDownOpen = this.RecevieCompanyCb.ItemsSource != null;
                this.RecevieCompanyCb.ItemsSource = data;
            }
        }
        private void ReceiverCompanyCb_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (RecevieCompanyCb.SelectedIndex > -1) isSelectReceiveCompany = true;
        }
        #endregion

        #region Material 
        private bool isSelectMaterial = false;
        private void MaterialNameCb_TextChanged(object sender, TextChangedEventArgs e)
        {
            String text = this.materialCb.Text.Trim();
            if (text.Length < 2)
            {
                isSelectMaterial = false;
                this.materialCb.IsDropDownOpen = false;
            }
            if (isSelectMaterial == true) { return; }
            if (text.Length >= 2)
            {
                List<Material> list = MaterialDao.Instance.SelectByNameOrFirstCaseLike(text);
                var data = list?.Select((i) => { return new MaterialVo(i); }).ToList();
                isSelectMaterial = list?.Count > 0;
                this.materialCb.ItemsSource = data;
                this.materialCb.IsDropDownOpen = this.materialCb.ItemsSource != null;
            }
        }

        private void MaterialNameCb_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (materialCb.SelectedIndex > -1) isSelectMaterial = true;
        }
        #endregion

        #region CarInfo Selection
        private bool isCarInfoSelectioned = false;
        private void CarNumberCb_TextChanged(object sender, TextChangedEventArgs e)
        {
            String text = this.carNumberCb.Text.Trim();
            if (text.Length <= 0)
            {
                isCarInfoSelectioned = false;
                this.carNumberCb.IsDropDownOpen = false;
                return;
            }
            if (isCarInfoSelectioned == true) { return; }

            if (text.Length >= 2)
            {
                List<CarInfo> list = CarDao.Instance.SelectByLike(text);
                var data = list?.Select((i) => { return new CarInfoVo(i); }).ToList();
                isCarInfoSelectioned = list?.Count > 0;
                this.carNumberCb.ItemsSource = data;
                this.carNumberCb.IsDropDownOpen = carNumberCb.ItemsSource != null;
            }
        }

        private void CarNumberCb_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (carNumberCb.SelectedIndex > -1) isCarInfoSelectioned = true;
        }
        #endregion

        #region 动态显示列

        private Dictionary<Int32, CheckBox> columnsDics;
        private void ColumnsSettingBtn_Click(object sender, RoutedEventArgs e)
        {
            if (columnsDics == null)
            {
                columnsDics = new Dictionary<int, CheckBox>();
                var cols = this.DataGrid.Columns;
                var style = FindResource("CheckBoxBaseStyle") as Style;
                for (int i = 0; i < cols.Count; i++)
                {
                    var col = cols[i];
                    var cb = new CheckBox()
                    {
                        Style = style,
                        Content = col.Header,
                        HorizontalAlignment =System.Windows. HorizontalAlignment.Left,
                        Tag = i,
                        Margin = new Thickness(20, 5, 20, 5),
                    };
                    if (col.Visibility == Visibility)
                    {
                        cb.IsChecked = true;
                    }
                    else
                    {
                        cb.IsChecked = false;
                    }
                    cb.Checked += ColumnCheckBox_Checked;
                    cb.Unchecked += ColumnCheckBox_Unchecked;
                    columnsDics.Add(i, cb);
                }
                foreach (var item in columnsDics)
                {
                    this.columnsPanel.Children.Add(item.Value);
                }
            }
            this.columnPopup.IsOpen = !columnPopup.IsOpen;
        }
        private void ColumnCheckBox_Checked(object sender, RoutedEventArgs e)
        {
            if (sender is CheckBox cb)
            {
                var index = (int)cb.Tag;
                var col = this.DataGrid.Columns[index];
                if (col != null)
                {
                    col.Visibility = Visibility.Visible;
                }
            }
        }

        private void ColumnCheckBox_Unchecked(object sender, RoutedEventArgs e)
        {
            if (sender is CheckBox cb)
            {
                var index = (int)cb.Tag;
                var col = this.DataGrid.Columns[index];
                if (col != null)
                {
                    col.Visibility = Visibility.Collapsed;
                }
            }
        }

        #endregion

        #region DataGrid
        private void DataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            e.Row.Header = e.Row.GetIndex() + 1;
        }

        private void DataGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {

        }
        #endregion


        private void WeighingTypeRb_Checked(object sender, RoutedEventArgs e)
        {
            if (!IsLoaded) return;

            var rb = sender as RadioButton;
            if (rb != null)
            {
                switch (rb.Name)
                {
                    case buyName:
                        this.billType = WeightingBillType.Buy;
                        break;
                    case SaleName:
                        this.billType = WeightingBillType.Sale;
                        break;
                    case SelfName:
                        this.billType = WeightingBillType.Self;
                        break;
                    case foreginName:
                        this.billType = WeightingBillType.Foreign;
                        break;
                    default:
                        this.billType = WeightingBillType.Buy;
                        break;
                }

                string text = rb.Content + "汇总报表";
                this.exclSubHeader.Text = text;

                LoadData();
            }
        }

        private void SetYearAndMonthItems()
        {
            //year
            int startYear = 2022; //从2022 系统诞生于2022
            int endYear = DateTime.Now.Year;
            if (startYear == endYear)
            {
                this.yearCb.Items.Add(endYear.ToString());
            }
            else
            {
                for (int i = startYear; i <= endYear; i++)
                {
                    this.yearCb.Items.Add(i.ToString());
                }
            }
            //month
            if (ReportType == MonthReport && this.monthCb.Visibility == Visibility.Visible)
            {
                int startMonth = 1;
                int endMonth = 12;
                if (startYear == endYear)
                {
                    endMonth = DateTime.Now.Month;
                }
                //month            
                this.monthCb.Items.Clear();
                for (int i = startMonth; i <= endMonth; i++)
                {
                    this.monthCb.Items.Add(i.ToString());
                }
            }
            if (ReportType == DayReport && this.DayCb.Visibility == Visibility.Visible)
            {
                int startMonth = 1;
                int endMonth = 12;
                if (endYear == DateTime.Now.Year)
                {
                    endMonth = DateTime.Now.Month;
                }
                //month            
                this.monthCb.Items.Clear();
                for (int i = startMonth; i <= endMonth; i++)
                {
                    this.monthCb.Items.Add(i.ToString());
                }

                //day
                this.DayCb.Items.Clear();
                int startday = 1;
                int endday;
                if (endMonth <= DateTime.Now.Month)
                {
                    endday = CommonFunction.GetDaysInMonth(endYear, endMonth);
                }
                else
                {
                    endday = DateTime.Now.Day;
                }
                for (int i = startday; i <= endday; i++)
                {
                    this.DayCb.Items.Add(i.ToString());
                }
            }
        }
        private void ChangeYearItems()
        {
            this.yearCb.SelectedIndex = -1;
            this.yearCb.Items.Clear();
            //year
            int startYear = 2022; //从2022 系统诞生于2022
            int endYear = DateTime.Now.Year;
            if (startYear == endYear)
            {
                this.yearCb.Items.Add(endYear.ToString());
            }
            else
            {
                for (int i = startYear; i <= endYear; i++)
                {
                    this.yearCb.Items.Add(i.ToString());
                }
            }
        }
        private void ChangeMonthItems(int currYear)
        {
            int endMonth = 12;
            int endYear = DateTime.Now.Year;
            if (endYear > currYear)
            {
                endMonth = 12;
            }
            else
            {
                endMonth = DateTime.Now.Month;
            }
            //month
            if (ReportType == MonthReport && this.monthCb.Visibility == Visibility.Visible)
            {
                int startMonth = 1;
                this.monthCb.Items.Clear();
                for (int i = startMonth; i <= endMonth; i++)
                {
                    this.monthCb.Items.Add(i.ToString());
                }
            }
        }

        private void ChangeDayItems(int year, int currMonth)
        {
            int endDay;
            int endMonth = DateTime.Now.Month;
            int endYear = DateTime.Now.Year;
            if (year >= endYear && endMonth > currMonth)
            {
                endDay = CommonFunction.GetDaysInMonth(year, currMonth);
            }
            else
            {
                endDay = DateTime.Now.Day;
            }
            //day
            if (ReportType == DayReport && this.DayCb.Visibility == Visibility.Visible)
            {
                int startDay = 1;
                this.DayCb.Items.Clear();
                for (int i = startDay; i <= endDay; i++)
                {
                    this.DayCb.Items.Add(i.ToString());
                }
            }
        }

        #region build weighing type button

        private void BuildWeighingTypeRadioButton()
        {
            //以 XAML　静态处理了        
        }


        #endregion

        //年份选择
        private void YearCb_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.IsLoaded && this.yearCb.SelectedIndex > -1)
            {
                if (ReportType == YearReport)
                {
                    LoadData();
                    int year = Convert.ToInt32(this.yearCb.SelectedItem.ToString());
                    this.dateRangeTb.Text = year.ToString() + "年";
                    SetSubTitleText(year.ToString() + "年");
                }
                else
                {
                    int year = Convert.ToInt32(this.yearCb.SelectedItem);
                    ChangeMonthItems(year);
                    //select last 
                    this.monthCb.SelectedIndex = this.monthCb.Items.Count - 1;
                }
            }
        }
        //月份选择
        private void MonthCb_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.IsLoaded && this.monthCb.IsVisible && this.monthCb.SelectedIndex > -1)
            {
                int month = Convert.ToInt32(this.monthCb.SelectedItem);
                int year = Convert.ToInt32(this.yearCb.SelectedItem);
                if (year == DateTime.Now.Year && month > DateTime.Now.Month)
                {
                    ChangeDayItems(year, month);
                    //select last item again
                    this.DayCb.SelectedIndex = this.DayCb.Items.Count - 1;
                }
                else if (ReportType == MonthReport)
                {
                    var str = year.ToString() + "年 " + month + " 月";
                    this.dateRangeTb.Text = str;
                    SetSubTitleText(str);
                    LoadData();
                }
                else
                {
                    ChangeDayItems(year, month);
                    //select last 
                    this.DayCb.SelectedIndex = this.DayCb.Items.Count - 1;
                }
            }
        }
        //天 选择
        private void DayCb_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.IsLoaded && this.DayCb.IsVisible && this.DayCb.SelectedIndex > -1)
            {
                int month = Convert.ToInt32(this.monthCb.SelectedItem);
                int year = Convert.ToInt32(this.yearCb.SelectedItem);
                int day = Convert.ToInt32(this.DayCb.SelectedItem);
                var str = year.ToString() + "年 " + month + " 月" + day + " 日";
                this.dateRangeTb.Text = str;
                SetSubTitleText(str);
                LoadData();
            }
        }

        private void SetSubTitleText(String datestr)
        {
            if (datestr.IsNullOrEmpty()) return;
            string t = this.exclSubHeader.Text;
            string afterSub = t.Substring(t.Length - 4);
            string preT;
            if (this.SelfWeighingRb.IsChecked == true)
            {
                preT = t.Substring(0, 6);
            }
            else
            {
                preT = t.Substring(0, 4);
            }
            var r = preT + " (" + datestr + ") " + afterSub;
            this.exclSubHeader.Text = r;
        }
        private void StartSeatchBtn_Click(object sender, RoutedEventArgs e)
        {
            LoadData();
        }
        private void LoadData()
        {
            Growl.Clear();
            if (billType == WeightingBillType.UNKNOWN)
            {
                var msg = "未选择过磅类型";
                Growl.Info(msg);
                return;
            }
            if (this.yearCb.SelectedIndex < 0)
            {
                var msg = "未选择年份";
                Growl.Info(msg);
                return;
            }
            if (ReportType == MonthReport)
            {
                if (this.ByMonthRb.IsChecked == true)
                {
                    if (this.monthCb.SelectedIndex < 0)
                    {
                        var msg = "未选择月份";
                        Growl.Info(msg);
                        return;
                    }
                }
                else
                {
                    //开始和结束时间
                    var startDate = this.startDatePicker.SelectedDate;
                    if (startDate == null)
                    {
                        var msg = "未选择开始日期";
                        Growl.Clear();
                        Growl.Info(msg);
                        return;
                    }
                    var endDate = this.endDatePicker.SelectedDate;
                    if (endDate == null)
                    {
                        var msg = "未选择结束日期";
                        Growl.Clear();
                        Growl.Info(msg);
                        return;
                    }
                }

            }
            if (ReportType == DayReport)
            {
                if (this.monthCb.SelectedIndex < 0)
                {
                    var msg = "未选择月份";
                    Growl.Info(msg);
                    return;
                }
                if (this.DayCb.SelectedIndex < 0)
                {
                    var msg = "未选择几号";
                    Growl.Info(msg);
                    return;
                }
            }
            int year = Convert.ToInt32(this.yearCb.SelectedItem);
            int month = 0;
            int day = 0;
            if (ReportType == MonthReport) month = Convert.ToInt32(this.monthCb.SelectedItem);

            if (ReportType == DayReport)
            {
                month = Convert.ToInt32(this.monthCb.SelectedItem);
                day = Convert.ToInt32(this.DayCb.SelectedItem);
            }

            // string condition = BuildCondition();
            long sendCompanyId = 0;
            if (this.sendCompanyCb.SelectedItem is CompanyVo vo)
            {
                sendCompanyId = vo.Id;
            }
            long receiveCompanyId = 0;
            if (this.RecevieCompanyCb.SelectedItem is CompanyVo rvo)
            {
                receiveCompanyId = rvo.Id;
            }
            long materialId = 0;
            if (this.materialCb.SelectedItem is MaterialVo mvo)
            {
                materialId = mvo.Id;
            }
            string carNumber = "";
            if (this.carNumberCb.SelectedItem is CarInfoVo cvo)
            {
                carNumber = cvo.CarNumber;
            }
            bool isGroupSend = (bool)this.SendGroupComopanyCb.IsChecked;
            bool isGroupReceive = (bool)this.ReceiveGroupComopanyCb.IsChecked;
            bool isGroupMaterial = (bool)this.MaterialGroupCb.IsChecked;
            bool isGroupCar = (bool)this.CarGroupCb.IsChecked;
            bool isGroupMonth = (bool)this.GroupMonthCb.IsChecked;
            bool isGroupDay = (bool)this.GroupDayCb.IsChecked;

            List<WeighingBillSummaryReportVo> reportVos;

            if (ReportType == MonthReport)
            {
                if (this.ByTimeRangeRb.IsChecked == true)
                {
                    var startDate = this.startDatePicker.SelectedDate;
                    if (startDate == null)
                    {
                        var msg = "未选择开始日期";
                        Growl.Clear();
                        Growl.Info(msg);
                        return;
                    }
                    var endDate = this.endDatePicker.SelectedDate;
                    if (endDate == null)
                    {
                        var msg = "未选择结束日期";
                        Growl.Clear();
                        Growl.Info(msg);
                        return;
                    }

                    string startDateStr = startDate?.ToString(Constant.DateFormat) + " 00:00:00";
                    string endDateStr = endDate?.ToString(Constant.DateFormat) + " 23:59:59";

                    reportVos = WeighingBillDao.Instance.SelectSummaryReportsInDate(billType,
                            startDateStr, endDateStr,
                            sendCompanyId, receiveCompanyId, materialId, carNumber, isGroupSend,
                            isGroupReceive, isGroupMaterial, isGroupCar);                 
                }
                else
                {
                    reportVos = WeighingBillDao.Instance.SelectSummaryReports(billType, year, month, day,
                               sendCompanyId, receiveCompanyId, materialId, carNumber, isGroupSend
                               , isGroupReceive, isGroupMaterial, isGroupCar, isGroupMonth, isGroupDay);
                }
            }
            else
            {
                reportVos = WeighingBillDao.Instance.SelectSummaryReports(billType, year, month, day,
                           sendCompanyId, receiveCompanyId, materialId, carNumber, isGroupSend,
                           isGroupReceive, isGroupMaterial, isGroupCar, isGroupMonth, isGroupDay);
            }

            //以下代码是json 储存 的测试 已经通过
            //var reportVos = WeighingBillTest.WeighingBillSummaryReportVos(billType, year, month,day,
            //    sendCompanyId, receiveCompanyId, materialId, carNumber, isGroupSend,
            //    isGroupReceive, isGroupMaterial, isGroupCar,isGroupMonth,isGroupDay);

            this.DataGrid.ItemsSource = null;
            this.DataGrid.ItemsSource = reportVos;

            CalcTotal();
        }
        // 计算 总数
        private void CalcTotal()
        {
            string unit = App.Config.WeighingConfig.Unit == Unit.Tone ? "吨" : "千克";
            string temp = "合计:共({0})车，净重({1}){2}。";
            List<WeighingBillSummaryReportVo>? reportVos = this.DataGrid.ItemsSource as List<WeighingBillSummaryReportVo>;
            if (reportVos == null || reportVos.Count == 0)
            {
                var t = string.Format(temp, 0, 0, unit);
                this.summaryTextBlock.Text = t;
            }
            else
            {
                long cars = 0;
                decimal tones = new decimal(0);

                foreach (var item in reportVos)
                {
                    cars += item.Cars;
                    tones += item.NetWeight;
                }
                this.summaryTextBlock.Text = string.Format(temp, cars, tones, unit);
            }

        }
        #region no used
        // no used ,becouse only mysql db effictive
        private string BuildCondition()
        {
            //time
            string condition = "";
            string timeLike = "";
            int year = Convert.ToInt32(this.yearCb.SelectedItem);
            int month = 0;
            timeLike = $"{year}%";
            if (ReportType == MonthReport)
            {
                month = Convert.ToInt32(this.monthCb.SelectedItem);
                timeLike = $"{year}-{month}%";
            }
            condition = timeLike;
            //send company
            if (this.sendCompanyCb.SelectedIndex > -1)
            {
                CompanyVo sendvo = this.sendCompanyCb.SelectedItem as CompanyVo;
                if (sendvo != null)
                {
                    var s = $" and {WeighingBillFiled.send_company_id} = {sendvo.Id}";
                    condition += s;
                }
            }
            //receive company
            if (this.RecevieCompanyCb.SelectedIndex > -1)
            {
                CompanyVo receiveVo = this.RecevieCompanyCb.SelectedItem as CompanyVo;
                if (receiveVo != null)
                {
                    var s = $" and {WeighingBillFiled.receive_company_id} = {receiveVo.Id}";
                    condition += s;
                }
            }
            //material 
            if (this.materialCb.SelectedIndex > -1)
            {
                MaterialVo materialVo = this.materialCb.SelectedItem as MaterialVo;
                if (materialVo != null)
                {
                    var s = $" and {WeighingBillFiled.material_id} = {materialVo.Id}";
                    condition += s;
                }
            }
            //car 
            if (this.carNumberCb.SelectedIndex > -1)
            {
                CarInfoVo carInfoVo = this.carNumberCb.SelectedItem as CarInfoVo;
                if (carInfoVo != null)
                {
                    var s = $" and {WeighingBillFiled.car_number} = '{carInfoVo.CarNumber}'";
                    condition += s;
                }
            }
            return condition;
        }
        // no used ,becouse only mysql db effictive
        private string BuildGroup()
        {
            string group = $"{WeighingBillFiled.out_time}";

            return "";
        }
        #endregion


        #region 导出
        private void ExportBtn_Click(object sender, RoutedEventArgs e)
        {
            string title = App.CurrentCompanyVo.FullName;
            string prx = this.yearCb.SelectedItem.ToString();
            int month = 0;
            if (ReportType == MonthReport)
            {
                month = Convert.ToInt32(this.monthCb.SelectedItem);
                prx += "-" + month + " ";
            }
            string subtitle = prx + this.exclSubHeader.Text;
            string filename = subtitle;
            ExclHelper.ExclExprotToExcelWitchStatisticInfo(
                               this.DataGrid,
                               filename,
                               title,
                               subtitle,
                               this.dateRangeTb.Text + "     导出时间:" + DateTimeHelper.getCurrentDateTime(),
                               this.summaryTextBlock.Text,
                               new List<string>());
        }

        #endregion

        #region 按月 或者 天 分组

        private void GroupMonthCb_Checked(object sender, RoutedEventArgs e)
        {
            OrtherUnChecked();
        }

        private void GroupDayCb_Checked(object sender, RoutedEventArgs e)
        {
            OrtherUnChecked();
        }

        private void OrtherUnChecked()
        {
            this.SendGroupComopanyCb.IsChecked = false;
            this.ReceiveGroupComopanyCb.IsChecked = false;
            this.MaterialGroupCb.IsChecked = false;
            this.CarGroupCb.IsChecked = false;
        }
        #endregion

        #region 其它分组

        private void GroupOrtherCb_Checked(object sender, RoutedEventArgs e)
        {
            this.GroupMonthCb.IsChecked = false;
            this.GroupDayCb.IsChecked = false;

        }
        #endregion

        private void ByMonthRb_Checked(object sender, RoutedEventArgs e)
        {
            this.monthCb.Visibility = Visibility.Visible;

        }

        private void ByTimeRangeRb_Checked(object sender, RoutedEventArgs e)
        {
            this.monthCb.Visibility = Visibility.Collapsed;
        }

        #region 将本机使用的基础数据设置默认数据源 最新使用的排在最前面
        /// <summary>
        /// 将本机使用的基础数据设置默认数据源 
        /// </summary>
        private void SetDefaultSource()
        {
            var companyS = CompanyDao.Instance.GetList().Select(i => { return new CompanyVo(i); }).ToList();
            var ms = MaterialDao.Instance.GetList().Select(i => { return new MaterialVo(i); }).ToList();
            var cars = CarDao.Instance.GetList().Select(i => { return new CarInfoVo(i); }).ToList();
            this.sendCompanyCb.ItemsSource = companyS;
            this.RecevieCompanyCb.ItemsSource = companyS;
            this.materialCb.ItemsSource = ms;
            this.carNumberCb.ItemsSource = cars;
        }

        #endregion

    }
}
